package cn.larry.common.utils;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This is created,
 * but not necessarily updated by me.
 * Therefore, AUTHOR is useless.
 * 2022/12/28 20:41
 *
 * @Version 1.0
 */
@Slf4j
public class AgentUserUtils {

    private static String pattern = "^Mozilla/\\d\\.\\d\\s+\\(+.+?\\)";
    private static String pattern2 = "\\(+.+?\\)";
    private static Pattern r = Pattern.compile(pattern);
    private static Pattern r2 = Pattern.compile(pattern2);

    public static String getDeviceInfo(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return getDeviceInfo(userAgent);
    }

    public static String getDeviceInfo(String userAgent) {
        Matcher m = r.matcher(userAgent);
        String result = null;
        if (m.find()) {
            result = m.group(0);
        }

        Matcher m2 = r2.matcher(result);
        if (m2.find()) {
            result = m2.group(0);
        }
        result = result.replace("(", "");
        result = result.replace(")", "");
        return filterDeviceInfo(result);
    }

    public static String filterDeviceInfo(String result) {
        if (StringUtils.isBlank(result)) {
            return null;
        }
        result = result.replace(" U;", "");
        result = result.replace(" zh-cn;", "");
        return result;
    }

    /**
     * 获取手机型号和网络状态
     *
     * @return  "MobilePhoneModel"｜"NetType"
     */
    public static String getMobilePhoneModelAndNetType(String userAgent) {
        String result = "";
        try {
            String deviceInfo = getDeviceInfo(userAgent);
            if(deviceInfo.contains("/")){// Android
                result = getDeviceInfo(userAgent).split("/")[1].split(";")[0];
            }else {// iPhone
                result = getDeviceInfo(userAgent).split("CPU")[1].trim();
            }
            String netType = "";
            try {
                netType = userAgent.split("NetType/")[1].split(" ")[0];
            } catch (Exception e) {
                log.error(userAgent + " [NetType解析异常]", e);
                netType = "未知";
            }
            result += "&" + netType;
        } catch (Exception e) {
            log.error("解析请求客户端信息异常", e);
            result = "未知&未知";
        }
        return result;
    }

    /*public static void main(String[] args) {
//        System.out.println(getMobilePhoneModelAndNetType(
//                "Mozilla/5.0 (Linux; Android 12; LSA-AN00 Build/HONORLSA-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.99 XWEB/4375 MMWEBSDK/20221012 Mobile Safari/537.36 MMWEBID/586 MicroMessenger/8.0.30.2260(0x28001E55) WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64"));
//     System.out.println(getMobilePhoneModelAndNetType(
//                "Mozilla/5.0 (iPhone; CPU iPhone OS 16_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.31(0x18001f34) NetType/4G Language/zh_CN"));
    System.out.println(getMobilePhoneModelAndNetType(
                "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"));
    }*/

}
